<?php

$nb_max="20"; // number of adsm per page

$page['adsm']=array();
$page['adsm_first_page']="";
$page['adsm_previous_page']="";
$page['adsm_last_page']="";
$page['adsm_next_page']="";
$page['L_message']="";

# suppression
if($right_user['delete_adsm'] AND isset($_GET['v2']) AND $_GET['v2']=="delete" AND isset($_GET['v3']) AND !empty($_GET['v3'])) 
{
 $var['id']=$_GET['v3'];
 $sql_sup=sql_replace($sql['adsm']['delete_ads'],$var);
 $sgbd = sql_connect();

 if(sql_query($sql_sup) != false) { $page['L_message']=$lang['adsm']['form_adsm_sup_1']; }
 else { $page['L_message']=$lang['adsm']['form_adsm_sup_0']; }

 sql_close($sgbd);
}


# TRI
# $_GET['v1'] is a variable like : page_1_name_asc
if(isset($_GET['v1']) AND preg_match("#page#",$_GET['v1']))
{
 $v=explode("_",$_GET['v1']);
 $page_num=$v['1'];
 $tri=$v['2'];
 $ordre=$v['3'];
}
else
{
 $page_num=1; // number of the page
 $tri="name"; // tri par defaut
 $ordre="asc"; // ordre par defaut
}

# ORDRE (sens)
if($ordre=="desc") { $sens="desc"; $sens_inv="asc"; }
else { $sens="asc"; $sens_inv="desc"; }


/************************/
/* START CONDITIONS  */
/************************/
if(!isset($var['condition']) OR $var['condition']=="")
{
 $condition=array();

 # creation of conditions list
 $nb_condition=sizeof($condition);
 if($nb_condition==0) { $var['condition']=""; }
 elseif($nb_condition=="1") { $var['condition']="WHERE ".$condition['0']; }
 else { $var['condition']="WHERE ".implode(" AND ",$condition); }
}
/**********************/
/* END OF CONDITIONS    */
/**********************/


/**********************/
/* ORDER (tri) */
/**********************/
if(!isset($var['order']))
{
 switch($tri) {
  case "name" : $var['order']=" ORDER BY ads_name ".$sens." "; break;
  default : $var['order']=" ORDER BY ads_name ".$sens." ";
 }
}

$page['page']=array();
if(!isset($var['limit']))
{
 /* we get the number of adsm */
 $sql_nb=sql_replace($sql['adsm']['get_ads_nb_total'],$var);
 $sgbd = sql_connect();
 $res_nb = sql_query($sql_nb);
 $ligne=sql_fetch_array($res_nb);
 $nb=$ligne['nb'];
 sql_free_result($res_nb);
 sql_close($sgbd);


 /***************/
 /* PAGINATION */
 /**************/
 # number of the current page
 $var['limit']="LIMIT ".($page_num-1)*$nb_max.",".$nb_max;
 $nb_page=ceil($nb/$nb_max);

 $url="index.php?r=".$plugin_idurl."&v1=page_";
 $end_url="_".$tri."_".$sens;

 $page['page']=generate_pagination($url, $nb_page,$page_num,$end_url);

 # previous page (except on the first one)
 if($page_num!=1)
 {
  $page['adsm_first_page']=convert_url($url."1".$end_url);
  $page['adsm_previous_page']=convert_url($url.($page_num - 1).$end_url);
 }
 # next page (except on the last one)
 if($page_num!=$nb_page)
 {
  $page['adsm_last_page']=convert_url($url.$nb_page.$end_url);
  $page['adsm_next_page']=convert_url($url.($page_num + 1).$end_url);
 }
 /******************/
 /* END PAGINATION */
 /******************/

}


$sql_adsm=sql_replace($sql['adsm']['get_filtered_ads'],$var);

$sgbd = sql_connect();
$res_adsm = sql_query($sql_adsm);
$nb_ligne=sql_num_rows($res_adsm);

if(!$right_user['adsm_list']) {
	$page['L_message_club']=$lang['general']['acces_reserve_admin'];
}
elseif($nb_ligne=="0")
{
 $page['L_message']=$lang['adsm']['E_adsm_not_found'];
}
else
{
 $i="0";
 while($ligne = sql_fetch_array($res_adsm))
 {
  $page['adsm'][$i]['id']=$ligne['ads_id'];
  $page['adsm'][$i]['name']=$ligne['ads_name'];
  $page['adsm'][$i]['url']=$ligne['ads_url'];
  $page['adsm'][$i]['file_content']=$ligne['ads_file'];
  $page['adsm'][$i]['pub_date']=$ligne['ads_publish_date'];
  $page['adsm'][$i]['end_date']=$ligne['ads_end_date'];
  $page['adsm'][$i]['description']=$ligne['ads_description']; 
  $page['adsm'][$i]['width']=$ligne['ads_width'];
  $page['adsm'][$i]['height']=$ligne['ads_height']; 
  $page['adsm'][$i]['mod']=$i%2;

  $page['adsm'][$i]['L_show_view']=$lang['adsm']['show_view'];
  $page['adsm'][$i]['adsm_view']=convert_url("index.php?r=".$plugin_idurl."&v1=view&v2=".$ligne['ads_id']);

 if(isset($var['value_adsm']) AND $var['value_adsm']==$ligne['adsm_id']) { $page['adsm'][$i]['selected']="selected"; } else { $page['adsm'][$i]['selected']=""; }
   
  $page['adsm'][$i]['adsm_edit']="";
  $page['adsm'][$i]['adsm_delete']="";
   if($right_user['edit_adsm'])
  {
   $page['adsm'][$i]['adsm_edit']=convert_url("index.php?r=".$plugin_idurl."&v1=form_adsm&v2=".$ligne['ads_id']); 
  }
  if($right_user['delete_adsm'])
  {
   $page['adsm'][$i]['adsm_delete']=convert_url("index.php?r=".$plugin_idurl."&v1=adsm_list&v2=delete&v3=".$ligne['ads_id']);
  }
  $i++;
 }
}
sql_free_result($res_adsm);
sql_close($sgbd);

if($right_user['add_adsm'])
 {
  $page['adsm_add']=convert_url("index.php?r=".$plugin_idurl."&v1=form_adsm");
  $page['admin']="1";
 }
else
{
 $page['adsm_add']="";
 $page['admin']="";
}

# text
$page['adsm_adsm']=convert_url("index.php?r=".$plugin_idurl."&v1=adsm_list");
$page['adsm_tri_name']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_name_".$sens_inv);
$page['adsm_tri_city']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_city_".$sens_inv);
$page['adsm_tri_place']=convert_url("index.php?r=".$plugin_idurl."&v1=page_".$page_num."_place_".$sens_inv);


$page['L_order']=$lang['adsm']['order_by'];
$page['L_name']=$lang['adsm']['name'];
$page['L_date']=$lang['adsm']['date'];

$page['L_first_page']=$lang['adsm']['first_page'];
$page['L_previous_page']=$lang['adsm']['previous_page'];
$page['L_next_page']=$lang['adsm']['next_page'];
$page['L_last_page']=$lang['adsm']['last_page'];

$page['L_add']=$lang['adsm']['add_adsm'];
$page['L_title']=$lang['adsm']['adsm_list'];

# meta
$page['meta_title']=$lang['adsm']['adsm_list'];
$page['template']=$tpl['adsm']['adsm_list'];


?>